﻿using Red.Controls.ViewModels;
using System;
using System.Collections.Generic;
using System.IO;

namespace Red.Toolkit.HoudiniDispatch
{
    /// <summary>
    /// Collection of Job Item Parameters Loaded from File
    /// </summary>
    public struct JobParameter
    {
        #region Data
        public string Name { get; set; }
        public string Type { get; set; }
        public string DefValue { get; set; }
        public string min { get; set; }
        public string max { get; set; }
        public string[] DefVec3 { get; set; }
        public string Priority { get; set; }
        private DateTime _CreationTime { get; set; }
        #endregion Data
    }

    /// <summary>
    /// Job Definitions with Parameter Lists for Manager to Handle
    /// </summary>
    public class JobDefinition
    {
        #region Data
        private readonly string _defFile;
        public readonly string name;
        public List<JobParameter> Params { get; set; }
        #endregion Data

        #region C-tor
        public JobDefinition(string FileName)
        {
            if ( null == Params )
            {
                Params = new List<JobParameter>();
            }
            
            System.IO.StreamReader jobReader = new System.IO.StreamReader( FileName );

            _defFile = FileName;
            name = Path.GetFileNameWithoutExtension( FileName );

            string line = "";
            while ( (line = jobReader.ReadLine()) != null )
            {
                line = line.Trim();
                if ( 3 < line.Length )
                { 
                    string[] splitLine = line.Split( '|' );
                    if ( 3 == splitLine.Length )
                    {
                        JobParameter jp = new JobParameter();
                        jp.Name = splitLine[0];
                        jp.Type = splitLine[1];
                        if("s"==jp.Type)
                            jp.DefValue = splitLine[2];
                        else if ( "i" == jp.Type )
                        {
                            string[] valSplit = splitLine[2].Split(';');
                            jp.DefValue = valSplit[0];
                            if ( 3 == valSplit.Length )
                            {
                                jp.min = valSplit[1];
                                jp.max = valSplit[2];
                            }
                            else
                            {
                                jp.min = "";
                                jp.max = "";
                            }
                        }
                        else if ( "v" == jp.Type )
                        {
                            string[] valSplit = splitLine[2].Split( ';' );
                            if ( 3 == valSplit.Length )
                            {
                                jp.DefVec3 = splitLine[2].Split( ';' );
                                jp.DefValue = splitLine[2];
                            }
                        }
                        Params.Add( jp );
                    }
                }
            }
        }
        #endregion C-tor
    }

    /// <summary>
    /// Main Job Manager Class
    /// </summary>
    static class HoudiniJobManager
    {
        #region Data
        public static List<JobDefinition> JobDefinitions;
        //public static List<HoudiniJob> HoudiniJobs;
        #endregion Data

        #region Job Loader and Getter
        public static List<JobDefinition> LoadJobDefinitions( DataItem parent )
        {
            if(null == JobDefinitions)
            {
                JobDefinitions = new List<JobDefinition>();
            }
            string[] jobTemplates = Directory.GetFiles( "Data\\JobTemplates" );
            foreach( string jobTemp in jobTemplates )
            {
                if ( File.Exists( jobTemp ) )
                {
                    JobDefinition jd = new JobDefinition( jobTemp );
                    JobDefinitions.Add( jd );
                }
            }
            return JobDefinitions;
        }

        public static List<string> GetJobDefinitionsList()
        {
            List<string> JDList = new List<string>();
            foreach( JobDefinition jd in JobDefinitions)
            {
                JDList.Add(jd.name);
            }
            return JDList;
        }
        #endregion Job Loader and Getter
    }
}
